//
//  BallAgeViewController.m
//  CoolMove
//
//  Created by CA on 15/9/16.
//  Copyright (c) 2015年 CA. All rights reserved.
//

#import "SportAgeViewController.h"
#import "NSDate+DateTools.h"

static NSString * const kDefaultNum = @"2";

@interface SportAgeViewController () <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UIView *popView;
@property (nonatomic, strong) NSArray *dataArr;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UIView *indicatorView;
@property (weak, nonatomic) IBOutlet UILabel *headDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *remindDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *YearDescriptionLabel;


@end

@implementation SportAgeViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureLocalization];
    [self addGestureRecognizer];
    [self updateThemeUI];
    [self registerNotification];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [MobClick beginLogPageView:@"BallAgePage"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [MobClick endLogPageView:@"BallAgePage"];
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Locailzation

- (void)configureLocalization
{
    [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    [self.okButton setTitle:NSLocalizedString(@"Ok", nil) forState:UIControlStateNormal];
    self.headDescriptionLabel.text = NSLocalizedString(@"Please choose training years", nil);
}

#pragma mark - Notification

- (void)registerNotification
{
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateThemeUI) name:kThemeManagerChangeThemeNotification object:nil];
}

#pragma mark - Getter/Setter

- (NSArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = @[NSLocalizedString(@"Less than 1 year", nil), NSLocalizedString(@"1 to 3 years", nil), NSLocalizedString(@"More than 3 years", nil)];
    }
    
    return _dataArr;
}

#pragma mark - IBAction

- (IBAction)cancel:(id)sender
{
    [self close];
}

- (IBAction)confirm:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(ballAgeViewController:didSelectRowDataString:index:)]) {
        NSInteger index = [self.picker selectedRowInComponent:0];
        NSString *dataString = self.dataArr[index];
        [self.delegate ballAgeViewController:self didSelectRowDataString:dataString index: index];
    }
    
    [self close];
}

#pragma mark - Private Method

- (void)updateThemeUI
{
//    self.okButton.backgroundColor = [[ThemeManager sharedInstance] pickerOkButtonColor];
//    self.popView.backgroundColor = [[ThemeManager sharedInstance] pickerBackgroundColor];
//    self.indicatorView.backgroundColor = [[ThemeManager sharedInstance] pickerSelectionIndicatorColor];
}

- (void)close
{
    [self dismissFromParentViewControllerWithAnimationType:DetailViewControllerAnimationTypeSlide];
}

#pragma mark - UIPicker DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.dataArr count];
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSAttributedString *attribuedString = [[NSAttributedString alloc] initWithString:self.dataArr[row] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:25.0f], NSForegroundColorAttributeName: [UIColor colorWithHex:0x7c7c7e]}];
    return attribuedString;
}

#pragma mark - Private Method

- (void)addGestureRecognizer
{
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
    gestureRecognizer.cancelsTouchesInView = NO;
    gestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:gestureRecognizer];
}
#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.view == touch.view;
}

@end
